<%@ include file="/common/common.jsp"%>
<%@ page language="java" pageEncoding="UTF-8"%>
<head>
<style>
/*模拟定位数据功能块样式*/
.imitate_div_css{
    height:100%;
    width:570px;
    margin-left:15px;
    /* border:1px solid orange; */
}
.imitate_div_css >div {
    margin-top:10px;
    width:100%;
    /* border:1px solid red; */
}
</style>


<script>
//初始化控件,页面加载时即加载，不需要调用
$(function(){
    //初始化表格
    $("#pl_imi_grid").datagrid({
        height:"100%",
		singleSelect:true,//单选
		rownumbers:true,//行号
		striped:true,//隔行变色
		fitColumns: false,//列的尺寸自动缩放
		checkOnSelect: false,//选中则勾选
		selectOnCheck: false,//勾选则选中
    });

});

//////////////////////////////////////////////////////////////变量
var pl_imi = {};//平台功能--模拟数据功能对象
pl_imi.debug = true;
pl_imi.cache = {};
pl_imi.cache.param = {};//缓存参数

//////////////////////////////////////////////////////////////初始化控件
//接口，初始化所有控件
pl_imi.initDivs = function(){
    this.initBasicDiv();
}

//初始化基础控件
pl_imi.initBasicDiv = function(){
    //车牌颜色
    DataUtil.initSlctDivByBasicData('pl_imi_plateColor','plateColor');
    $("#pl_imi_plateColor").combobox("select",2);

    //定位时间
    let data = [{text:'7天',value:7},{text:'10天',value:10},{text:'14天',value:14}];
    $("#pl_imi_nearTimeValue").combobox({
        textField: "text",
        valueField: "value",
        data: data,
        select: data[0].value
    });
    $("#pl_imi_nearTimeValue").combobox("select",7);

    //定位时间
    $("#pl_imi_gpsTime").datetimebox('setValue',Utility.now());

    //定位数量
    let numData = [{text:'1',value:1}];
    $("#pl_imi_pointNum").combobox({
        textField: "text",
        valueField: "value",
        data: numData,
        select: numData[0].value
    });
    $("#pl_imi_pointNum").combobox("select",1);
    
}



//////////////////////////////////////////////////////////////查找最后定位
//获取最后的定位信息
pl_imi.getLastPointData = function(){
    let that = this;
    that.cache.param = {};
    let param = that.getQueryParam();
    // that.outDebug("查询参数:" , param);
    that.checkParam(param);
    if(param.message == ""){
        that.outMsg('开始查询,参数为：' + DataUtil.obj2String(param));
        that.outMsg("请等待查询结果。。。。。。");
        Utility.getAjaxData(globalConfig.basSlctUrl,JSON.stringify(param),function(data){
            // that.outDebug("接收到最后定位数据信息：" ,data);
            let rows = [];
            if(data.success){
                if(data.data.length > 0){
                    rows.push(data.data[0]);
                    that.outMsg("查询到最后定位数据：" + DataUtil.obj2String(data.data[0]));
                    that.cache.param = param;
                }else{
                    that.outMsg("当前车牌号 " + param.nearTimeValue + " 天内无定位数据！",'red');
                    // DataUtil.showErrorMsg(param.nearTimeValue + "天内无当前车牌号定位数据！",'red');
                }
            }else{
                that.outMsg('查询最后定位数据失败：' + data.message,'red');
            }

            //填充数据
            $("#pl_imi_grid").datagrid('loadData',rows); 
        });
    }else{
        that.outMsg('无法查询：' + param.message,'red');
        DataUtil.showErrorMsg(param.message);
    }

    // this.test();
}


//获取查询参数
pl_imi.getQueryParam = function(){
    let param = {message:""};
    param.plateNo = $("#pl_imi_plateNo").val();

    let nearTimeValue = parseInt($("#pl_imi_nearTimeValue").combobox('getValue'));
    param.nearTimeValue = isNaN(nearTimeValue) ? 7 : nearTimeValue;

    param.gpsTime = $("#pl_imi_gpsTime").datetimebox('getValue');

    let pointNum = parseInt($("#pl_imi_pointNum").combobox('getValue'));
    param.pointNum = isNaN(pointNum) ? 1 : pointNum;

    param.plateColor = $("#pl_imi_plateColor").combobox('getValue');

    return param;
}

//校验并解析参数
pl_imi.checkParam = function(param){
    //校验
    if(param.plateNo == ""){
        param.message += "车牌号不能为空!";
    }

    if(param.gpsTime == ""){
        param.message += "定位时间不能为空!";
    }


    //参数正常，开始解析
    if(param.message == ""){
        param.sendTime = Utility.now();//结束时间

        //开始时间
        let start = new Date().getTime() - param.nearTimeValue * 24 *60 * 60 * 1000;
        param.sendTimeStart = Utility.dateToString(new Date(start));

        //查询语句
        param.queryId = "dataProcess.selectNearestGpsData";
    }

}

//测试方法
pl_imi.test = function(){
    let data = {
        rows: [
            {
                plateNo: '12345',
                simNo:123456,
                createDate:'2019-11-09 00:00:00'
            },
        ],
        length:1
    };
    $("#pl_imi_grid").datagrid('loadData',data);
}


//////////////////////////////////////////////////////////////插入转发模拟数据
//插入转发模拟数据
pl_imi.insertAndTransImiData = function(){
    let that = this;
    $.messager.confirm("确认","确定插入并转发模拟数据？",function(res){
        if(res){
            let param = that.getInsertAndTransParam();
            if(param.message == ""){
                that.outMsg("开始转发，转发参数为：" + DataUtil.obj2String(param));
                let url = globalConfig.webPath + "/insertAndTrans/insertGpsDatas.action";
                Utility.getAjaxData(url,JSON.stringify(param),function(data){
                    if(data.success){
                        that.outMsg(data.message + " , 插入并转发数量为： " + data.data );
                    }else{
                        that.outMsg("转发失败：" + data.message , 'red');
                    }
                });
            }else{
                that.outMsg('无法进行模拟数据插入和转发：' + param.message,'red');
            }
        }
    });
}

//获取插入转发参数
pl_imi.getInsertAndTransParam = function(){
    let param = {message:""};
    let cacheParam = this.cache.param;
    if(DataUtil.isDefined(cacheParam.plateNo)){
        param.message += "获取缓存参数失败！";
    }else{
        cacheParam.gpsTime = $("#pl_imi_gpsTime").datetimebox('getValue');
    }

    let data = $("#pl_imi_grid").datagrid('getRows'); 
    if(data.length <= 0){
        param.message += "当前没有定位数据";
    }

    if(param.message == ""){
        //配置参数：
        param.plateNo = cacheParam.plateNo;
        param.plateColor = cacheParam.plateColor;
        param.alarmState = 0;
        param.status = 790531;//车辆状态，Acc开（0），定位（1），车门枷锁（12），gps定位（18），北斗定位（19）
        param.gas = 0;
        param.valid = 1;//数据有效
        param.commandId = 0;
        

        //用于记录日志信息的数据
        param.insertCreateDate = Utility.dateToString(new Date());
        param.insertSatrtDate = cacheParam.gpsTime;//开始和结束时间都是定位时间
        param.insertEndDate = cacheParam.gpsTime;

        //插入和转发标记
        param.insertFlg = true;
        param.transFlg = false;//如果不需转发，则不加入这个字段
        param.transType = "new";
        param.transUrl = "rmi://222.187.86.218:18800/transferService";

        //数据
        let obj = data[0];
        if(obj){
            obj.sendTime = cacheParam.gpsTime;
            obj.createDate = obj.sendTime;
            obj.alarmState = param.alarmState;
            obj.status = param.status;
            obj.speed = obj.velocity;
            obj.recSpeed = obj.speed;
            obj.plateColor = param.plateColor;
            param.gpsList = JSON.stringify([obj]);//gps数据列表
            param.simNo = obj.simNo;
        }
        if(DataUtil.isDefined(param.gpsList)){
            param.message += "解析重构定位数据失败！";
        }
    }

    return param;
}



//////////////////////////////////////////////////////////////公用方法
pl_imi.outDebug = function(msg,data){
    if(this.debug == true){
        DataUtil.outDebug(msg,data);
    }
}


//显示信息到控件中
pl_imi.outMsg = function(msg,color){
    let colorStr = '';
    if(color == 'red'){
        colorStr = 'style="color:red;"';
    }
    let html = '<div ' + colorStr + '>';
    html += '&nbsp;&nbsp;&nbsp;&nbsp;[' + Utility.now() + ']&nbsp;&nbsp;';
    html += msg;
    html += '</div>';
    $("#pl_imi_msgDiv").prepend(html);
}

//////////////////////////////////////////////////////////////加载后执行
$(document).ready(function(){
    pl_imi.initDivs();
});
</script>



</head>
<body class="sh_body_Css">
    <div class="sh_body_first_par_div base_hori_flexStartDiv_css">
        <div class="imitate_div_css base_ver_flexStartDiv_css">
            <span style="margin-top:10px;"> 模拟gps定位数据</span>
            <div class="base_hori_spaceBetweenDiv_css" style="height:80px;">
                <!--左侧参数栏-->
                <div style="width:72%;height:100%;" class="base_ver_Around_rightDiv_css">
                    <div>
                        <span>车牌号:</span>
                        <input id="pl_imi_plateNo" type="text" style="width:80px;">
                        &nbsp;
                        <span>颜色:</span>
                        <select id="pl_imi_plateColor" style="width:60px;"></select>
                        &nbsp;
                        <span>最近定位时间:</span>
                        <select id="pl_imi_nearTimeValue" style="width:70px;"></select>
                    </div>
                    <div>
                        <span>定位时间:</span>
                        <input id="pl_imi_gpsTime" type="text" class="easyui-datetimebox" style="width:150px;">
                        &nbsp;
                        <span>定位数量：</span>
                        <select id="pl_imi_pointNum" style="width:90px;"></select>
                    </div>
                </div>
                <!--右侧按钮栏-->
                <div style="width:24%;height:100%;" class="base_ver_Around_leftDiv_css">
                    <a href="#" class="easyui-linkbutton" onclick="pl_imi.getLastPointData();">查询最后定位</a>
                    <a href="#" class="easyui-linkbutton" onclick="pl_imi.insertAndTransImiData();">插入转发模拟数据</a>
                </div>
            </div>
            <!--定位数据显示控件-->
            <div style="height:100px;overflow: auto;">
                <table id="pl_imi_grid" class="easyui-datagrid" style="width:100%;">
                    <thead>
                        <tr>
                            <th data-options="field:'plateNo'" width="70">车牌号</th>
                            <th data-options="field:'simNo'" width="120">simNo</th>
                            <th data-options="field:'sendTime'" width="130">定位时间</th>
                            <th data-options="field:'latitude'" width="70">纬度</th>
                            <th data-options="field:'longitude'" width="70">经度</th>
                            <th data-options="field:'velocity'" width="70">速度</th>
                            <th data-options="field:'mileage'" width="70">里程</th>
                            <th data-options="field:'createDate'" width="130">接收时间</th>
                        </tr>
                    </thead>
                </table>
            </div>
            <!--显示信息控件-->
            <div style="width:100%;height:180px;border-radius:4px;overflow:auto;border:1px solid rgb(210,210,210);"
                id="pl_imi_msgDiv">

            </div>
        </div>
    </div>
</body>